package org.example.service.impl;

import org.example.mapper.DepositTbMapper;
import org.example.pojo.DepositTb;
import org.example.service.api.DepositServiceApi;
import org.example.util.JSONResult;
import org.example.util.JSONStats;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//充值
@Service
public class DepositService implements DepositServiceApi<DepositTb> {
    //Dao
    @Autowired
    DepositTbMapper depositTbMapper;

    /**
     * 查询充值记录
     * @param depositId 充值记录id
     * @return
     */
    @Override
    public JSONResult adminQueryDepositById(Integer depositId) {
        JSONResult jsonResult = new JSONResult();
        DepositTb depositInfo =depositTbMapper.queryDepositInfoById(depositId);

        if(depositInfo!=null){
            //如果获取到的信息不是空
            //设置响应信息
            //查询失败
            jsonResult.setData(depositInfo);
            jsonResult.setStatusCode(JSONStats.QUERY_SUCCESS.getStatusCode());
            jsonResult.setMessage(JSONStats.QUERY_SUCCESS.getMessage());
        }else {
            //查询失败
            jsonResult.setMessage(JSONStats.QUERY_ERROR.getMessage());
            jsonResult.setStatusCode(JSONStats.QUERY_ERROR.getStatusCode());
        }
        return jsonResult;
    }

    /**
     * 通过寝室id删除充值记录
     * @param dormitoryId
     * @return
     */
    @Override
    public JSONResult adminDeleteDepositByDormitoryId(Integer dormitoryId) {
        JSONResult jsonResult=new JSONResult();
        int rows = depositTbMapper.deleteDepositByDormitoryId(dormitoryId);

        if(rows>0){
            //充值成功
            jsonResult.setMessage(JSONStats.DELETE_DEPOSIT_SUCCESS.getMessage());
            jsonResult.setStatusCode(JSONStats.DELETE_DORMITORY_SUCCESS.getStatusCode());

        }else {
            //充值失败
            jsonResult.setMessage(JSONStats.DELETE_DEPOSIT_ERROR.getMessage());
            jsonResult.setMessage(JSONStats.DELETE_DEPOSIT_ERROR.getMessage());
        }
        return jsonResult;
    }

    @Override
    public JSONResult adminDeleteDepositById(Integer depositId) {
        JSONResult jsonResult = new JSONResult();

        int rows= depositTbMapper.deleteDepositByDepositId(depositId);
        if(rows>0){
            //充值成功
            jsonResult.setMessage(JSONStats.DELETE_DEPOSIT_SUCCESS.getMessage());
            jsonResult.setStatusCode(JSONStats.DELETE_DORMITORY_SUCCESS.getStatusCode());

        }else {
            //充值失败
            jsonResult.setMessage(JSONStats.DELETE_DEPOSIT_ERROR.getMessage());
            jsonResult.setMessage(JSONStats.DELETE_DEPOSIT_ERROR.getMessage());
        }

        return jsonResult;
    }

    /**
     * 通过用id删除充值记录
     * @param userId 用户id
     * @return
     */
    @Override
    public JSONResult adminDeleteDepositByUserId(Integer userId) {
        JSONResult jsonResult=new JSONResult();
        int rows=depositTbMapper.deleteDepositByUserId(userId);

        if(rows>0){
            //充值成功
            jsonResult.setMessage(JSONStats.DELETE_DEPOSIT_SUCCESS.getMessage());
            jsonResult.setStatusCode(JSONStats.DELETE_DORMITORY_SUCCESS.getStatusCode());

        }else {
            //充值失败
            jsonResult.setMessage(JSONStats.DELETE_DEPOSIT_ERROR.getMessage());
            jsonResult.setMessage(JSONStats.DELETE_DEPOSIT_ERROR.getMessage());
        }
        return jsonResult;
    }

    /**
     * 增加充值记录
     * @param userId 用户id
     * @param dormitoryId 寝室id
     * @param userFee 充值费用
     * @return
     */
    @Override
    public JSONResult adminAddDepositMonkey(Integer userId, Integer dormitoryId, Integer userFee) {
        JSONResult jsonResult = new JSONResult();
        int rows=depositTbMapper.addDormitoryMonkey(userId,dormitoryId,userFee);
        int rows2=depositTbMapper.insertDepositInfo(userId,dormitoryId,userFee);
        if(rows>0 && rows2>0){


            jsonResult.setMessage(JSONStats.DESPOIT_MONKEY_OK.getMessage());
            jsonResult.setStatusCode(JSONStats.DESPOIT_MONKEY_OK.getStatusCode());
            //成功
        }else {
            //失败

            jsonResult.setMessage(JSONStats.DESPOIT_MONKEY_ERROR.getMessage());
            jsonResult.setStatusCode(JSONStats.DESPOIT_MONKEY_ERROR.getStatusCode());

        }


        return jsonResult;
    }

    /**
     * 充值记录修改
     * @param dormitoryId 寝室id
     * @param userId    用户id
     * @param userFee   用户费用
     * @param depositId 充值记录
     * @return
     */
    @Override
    public JSONResult adminAlterDepositInfo(Integer dormitoryId, Integer userId, Integer userFee, Integer depositId) {
        JSONResult jsonResult = new JSONResult();
        int rows=depositTbMapper.alterDepositInfo(dormitoryId,userId,userFee,depositId);
        if (rows > 0) {

            jsonResult.setStatusCode(JSONStats.ALTER_DEPOSIT_SUCCESS.getStatusCode());
            jsonResult.setMessage(JSONStats.ALTER_DEPOSIT_SUCCESS.getMessage());
        }else {
            jsonResult.setStatusCode(JSONStats.ALTER_DEPOSIT_ERROR.getStatusCode());
            jsonResult.setMessage(JSONStats.ALTER_DEPOSIT_ERROR.getMessage());
        }
        return jsonResult;
    }
}
